﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WordCloudGenerator.TextAnalyses.Processing;

namespace WordCloudInterface
{
    public partial class CustomizeWord : Form
    {
        private IWord word;
        private Interface parentForm;

        public CustomizeWord(Interface form1, IWord word)
        {
            this.parentForm = form1;
            this.word = word;

            InitializeComponent();

            // Change window upper-left icon
            this.Icon = Interface.GetProgramIcon();

            this.SetWindowTitle();
            this.DisplayWordStatistics();
            this.splitContainer1.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.splitContainer1.IsSplitterFixed = false;
            this.splitContainer1.FixedPanel = FixedPanel.None;
            
        }

        //
        // Set window title.
        //
        private void SetWindowTitle()
        {
            this.Text = string.Format("Statistics for word [{0}]", this.word.Text);
        }

        //
        // Fill word's statistics.
        //
        private void DisplayWordStatistics()
        {
            string[] occurences = this.word.GetCaption().Split("\r\n".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
            List<string> finalTxt = new List<string>();
            foreach (string str in occurences)
            {
                if (str.Length >= 3)
                {
                    string[] wordOccurence = str.Split("\t".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
                    if (wordOccurence.Length == 2)
                        finalTxt.Add("Number of occurence for word [" + wordOccurence[0] + "]: " + wordOccurence[1]);
                }
            }
            int i = 0;
            while (i < finalTxt.Count)
            {
                this.label1.Text += finalTxt[i];
                ++i;
                if (i < finalTxt.Count)
                    this.label1.Text += "\r\n";
            }
            this.splitContainer1.PerformAutoScale();
            this.PerformAutoScale();
        }

        //
        // Btn edit word.
        //
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.panel1.Visible == false)
            {
                this.textBox1.Text = this.word.Text;
                this.panel1.Visible = true;
                this.splitContainer1.PerformAutoScale();
                this.PerformAutoScale();
                this.Height += 100;
            }
        }

        //
        // Btn save word edition.
        //
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length > 0 && this.word.Text.CompareTo(this.textBox1.Text) != 0)
                this.parentForm.FromWordCloud_EditWord(this.word, this.textBox1.Text, this.word.Occurrences);
            this.Close();
        }

        //
        // Btn delete word.
        //
        private void button2_Click(object sender, EventArgs e)
        {
            this.parentForm.FromWordCloud_DeleteWord(this.word);
            this.Close();
        }

    }
}
